% function to compute EpE
% e-predicted hxhy-ex or hxhy-ey
% Reference : Ellinghaus, A. 1997. ProcMT User's manual. Metronix GmbH, Braunschweig, Germany
% latest date 20.05.2003


function[ZpZp] = pchan(a,b,data,flag),


% a & b are corresponding impedance (Zxx Zxy) or (Zyx Zyy)
% data is array of cross spectra - SPMmatrix
% 
%flag should be either 'Ex' or 'Ey';

j = sqrt(-1);

HxHx = data(1,1);
HyHy = data(2,2);
HzHz = data(3,3);
ExEx = data(4,4);
EyEy = data(5,5);

HxHy = data(2,1) - j*data(1,2);
HyHx = conj(HxHy);
HxEx = data(4,1) - j*data(1,4);
ExHx = conj(HxEx);
HxEy = data(5,1) - j*data(1,5);
EyHx = conj(HxEy);
ExHy = data(4,2) + j*data(2,4);
HyEx = conj(ExHy);
HyEy = data(5,2) - j*data(2,5);
EyHy = conj(HyEy);



if flag == 'Ex',
ZpZ = a*HxEx + b*HyEx;
elseif flag == 'Ey',
ZpZ = a*HxEy + b*HyEy;
end;

ZpX = a*HxHx + b*HyHx;
ZpY = a*HxHy + b*HyHy;

%ZpZp = a XZp + b YZp = a (ZpX) + b (ZpY) ;

ZpZp = a*conj(ZpX) + b*conj(ZpY);


if flag == 'Ex',
	ZpZp = ZpZp*ExEx;
elseif flag == 'Ey',
	ZpZp = ZpZp*EyEy;
end;

